package com.songtech.server.Jedis;

/**
 * Create By YINN on 2018/4/9 18:37
 * Description : Redis基础实现类
 */

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Service;


@Service("jedisBaseDaoImpl")
public class JedisBaseDaoImpl<T extends Serializable> extends AbstractJedisDaoImpl<String, T> implements JedisBaseDao<T> {



    /**
     * 添加对象
     */
    @Override
    public boolean put(String key, final T value) {

        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean put(String key,final T value, long time) {

        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }



    /**
     * 删除对象 ,依赖key
     */
    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 删除集合 ,依赖key集合
     */
    public void delete(List<String> keys) {
        redisTemplate.delete(keys);
    }

    /**
     * 添加对象
     */
    @Override
    public boolean putForHash(String namespace, String key, final T value) {

        boolean result = false;
        try {
            redisTemplate.opsForHash().put(namespace, key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除对象 ,依赖key
     */
    @Override
    public void deleteForHash(String namespace, String key) {

        System.out.println(redisTemplate.opsForHash().hasKey(namespace,key));


//        redisTemplate.opsForHash().

        redisTemplate.opsForHash().delete(namespace, key);
    }

    /**
     * 根据key获取对象
     */
    @Override
    public T getForHash(String namespace,String key) {
        return (T) redisTemplate.opsForHash().get(namespace, key);
    }

    /**
     * 根据key获取对象
     */
    @Override
    public T get(String key) {
        return redisTemplate.opsForValue().get(key);
    }



    /**************************************队列操作******************************************/

    /**
     * 入栈（右入栈）
     */
    public Long rpush(String key, T value) {
        return redisTemplate.opsForList().rightPush(key, value);
    }

    /**
     * 入栈（队头入栈）
     */
    public Long lpush(String key, T value){
        return redisTemplate.opsForList().leftPush(key, value);
    }


    /**
     * 出栈(左出栈)
     */
    public T lpop(String key){
//        redisTemplate.getConnectionFactory().getConnection().;
       return redisTemplate.opsForList().leftPop(key);
    }

    /**
     * 队列长度
     */
    public Long size(String key){
        return redisTemplate.opsForList().size(key);
    }

    /**
     * 范围检索
     */
    public List<T> range(String key, int start, int end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    /**
     * 移除
     */
    public void remove(String key, long i, T value) {
        redisTemplate.opsForList().remove(key, i, value);
    }

    /**
     * 检索
     */
    public T index(String key, long index) {
        return redisTemplate.opsForList().index(key, index);
    }

    /**
     * 裁剪
     */
    public void trim(String key, long start, long end) {
        redisTemplate.opsForList().trim(key, start, end);
    }

//    /**
//     * 添加对象
//     */
//    @Override
//    public boolean put(final String key,final T value) {
//
//        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
//            public Boolean doInRedis(RedisConnection connection)
//                    throws DataAccessException {
//                RedisSerializer serializer = getRedisSerializer();
//                return connection.setNX(serializer.serialize(key),
//                                        serializer.serialize(value));
//            }
//        });
//        return result;
//    }
//
//    /**
//     * 删除对象 ,依赖key
//     */
//    @Override
//    public void delete(String key) {
//        redisTemplate.delete(key);
//    }
//
//    /**
//     * 删除集合 ,依赖key集合
//     */
//    public void delete(List<String> keys) {
//        redisTemplate.delete(keys);
//    }
//
//    /**
//     * 修改对象
//     */
//    public boolean update(String key, final T value) {
//        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
//            public Boolean doInRedis(RedisConnection connection)
//                    throws DataAccessException {
//                RedisSerializer serializer = getRedisSerializer();
//                connection.set(serializer.serialize(key),
//                        serializer.serialize(value));
//                return true;
//            }
//        });
//        return result;
//    }
//
//
//    /**
//     * 根据key获取对象
//     */
//    @Override
//    public T get(final String key) {
//        T result = redisTemplate.execute(new RedisCallback<T>() {
//            public T doInRedis(RedisConnection connection)
//                    throws DataAccessException {
//                RedisSerializer serializer = getRedisSerializer();
//                return (T) serializer.deserialize(connection.get(serializer.serialize(key)));
//            }
//        });
//        return result;
//    }
//-----------------------------------------------------------



}